package com.lanxiao.cloud.common.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lanxiao.cloud.common.core.enums.DelFlagEnum;
import com.lanxiao.cloud.common.core.service.IOperatorService;
import java.util.Date;
import java.util.Objects;
import org.apache.ibatis.reflection.MetaObject;

/**
 * mybatis默认配置
 *
 * @author ：qiaoliang
 * @date ：2021-08-19
 */
public class ModelMetaObjectHandler implements MetaObjectHandler {

  private final IOperatorService operatorService;
  /**创建者**/
  private static final String CREATE_BY = "createBy";
  /**创建时间**/
  private static final String CREATE_TIME = "createTime";
  /**修改者**/
  private static final String UPDATE_BY = "updateBy";
  /**修改时间**/
  private static final String UPDATE_TIME = "updateTime";
  /**假删除**/
  private static final String DEL_FLAG = "delFlag";

  public ModelMetaObjectHandler(IOperatorService operatorService) {
    this.operatorService = operatorService;
  }

  @Override
  public void insertFill(MetaObject metaObject) {
    // 获取到需要被填充的字段值
    if(Objects.isNull(getFieldValByName(CREATE_TIME, metaObject))){
      setFieldValByName(CREATE_TIME, new Date(), metaObject);
    }
    if(Objects.isNull(getFieldValByName(CREATE_BY, metaObject))){
      setFieldValByName(CREATE_BY, operatorService.operator(), metaObject);
    }
    if(Objects.isNull(getFieldValByName(DEL_FLAG, metaObject))){
      setFieldValByName(DEL_FLAG, DelFlagEnum.NOT_DELETE, metaObject);
    }
  }

  @Override
  public void updateFill(MetaObject metaObject) {
    // 获取到需要被填充的字段值
    if(Objects.isNull(getFieldValByName(UPDATE_TIME, metaObject))){
      setFieldValByName(UPDATE_TIME, new Date(), metaObject);
    }
    if(Objects.isNull(getFieldValByName(UPDATE_BY, metaObject))){
      setFieldValByName(UPDATE_BY, operatorService.operator(), metaObject);
    }
  }
}
